package icasue.stronger.wareable.logicstronger.logicsupport;

import icasue.base.utils.wareable.CLog;
import icasue.stronger.wareable.logicstronger.StrongerAdvice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import icasue.reflect.handles.object.ObjectOF;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Parameter;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2020/6/28 下午5:18
 * @UpdateDate:
 * @Description: 方法调度日志逻辑
 */
@Slf4j
public class LoggerStrongerLogic extends StrongerAdvice {

    public LoggerStrongerLogic(){
        super();
        addBeforeMethodLogic( (proxyClass, invoker, method,params) -> {
            //获取URI
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if(ObjectOF.isNull_.test(requestAttributes)) { return null; }
            HttpServletRequest request = requestAttributes.getRequest();
            if(ObjectOF.isNull_.test(request)) { return null; }

            String uri = request.getRequestURI().toString();
            //构建方法调度日志
            StringBuilder builder = new StringBuilder()
                    .append("\n\t调度方法: ")
                    .append(method)
                    .append("\n\t调度者: ")
                    .append(invoker)
                    .append("\n\t调度参数: ");
            Parameter[] parameters = method.getParameters();
            Class<?>[] parameterTypes = method.getParameterTypes();
            for (int i = 0; i < parameters.length; i++) {
                builder.append("\n\t\t")
                        .append(parameterTypes[i].getName()).append("\t").append(params[i]);
            }
            CLog.info("\n\t调度信息: \n\tURI: %s %s",
                    uri ,
                    builder.toString()
            );
            return null;
        });
    }
}
